package com.example.campuslink.ui.activity

import android.media.MediaPlayer.OnPreparedListener
import android.net.Uri
import android.os.Handler
import android.os.Looper
import android.view.View
import android.widget.SeekBar
import android.widget.SeekBar.OnSeekBarChangeListener
import com.bumptech.glide.Glide
import com.example.campuslink.BR
import com.example.campuslink.R
import com.example.campuslink.commen.tools.putExtraKey
import com.example.campuslink.databinding.ActivityVideoPlayBinding
import com.example.campuslink.viewModel.VideoPlayViewModel

class VideoPlayActivity : BaseActivity<ActivityVideoPlayBinding, VideoPlayViewModel>(
    VideoPlayViewModel::class.java,
    R.layout.activity_video_play,
    BR.viewModel
) {
    private val handler = Handler(Looper.getMainLooper())
    override fun initView() {
        val path = intent.getStringExtra(putExtraKey.IMAGE_PATH.key) ?: ""
        mViewDataBinding?.ivBack?.setOnClickListener({
            finish()
        })
        val uri = Uri.parse(path)
        mViewDataBinding?.vvVideoView?.setVideoURI(uri)


        // 为 VideoView 设置准备好的监听器
        mViewDataBinding?.vvVideoView?.setOnPreparedListener(OnPreparedListener { // 设置 SeekBar 的最大值为视频的总时长
            mViewDataBinding?.seekProcess?.setMax(mViewDataBinding?.vvVideoView?.getDuration() ?: 0)
            // 开始更新进度条
            updateSeekBar()
        })


        // 为 SeekBar 设置拖动监听器
        mViewDataBinding?.seekProcess?.setOnSeekBarChangeListener(object : OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
                if (fromUser) {
                    // 用户拖动 SeekBar 时，跳转到相应的播放位置
                    mViewDataBinding?.vvVideoView?.seekTo(progress)
                }
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {
                // 开始拖动 SeekBar 时，暂停更新进度条
                handler.removeCallbacks(updateSeekBarRunnable)
            }

            override fun onStopTrackingTouch(seekBar: SeekBar) {
                // 停止拖动 SeekBar 时，继续更新进度条
                updateSeekBar()
            }
        })

        mViewDataBinding?.vvVideoView?.start()
        updateSeekBar()
        mViewModel?.setStatus()
        Glide.with(this).load(R.drawable.stop).into(mViewDataBinding?.ivPlayButton ?: return)
        mViewDataBinding?.ivPlayButton?.setOnClickListener(View.OnClickListener {
            if (mViewModel?.status?.value ?: false) {
                mViewDataBinding?.vvVideoView?.pause()
                handler.removeCallbacks(updateSeekBarRunnable)
                mViewModel?.setStatus()
                Glide.with(this@VideoPlayActivity).load(R.drawable.start)
                    .into(mViewDataBinding?.ivPlayButton ?: return@OnClickListener)
            } else {
                mViewDataBinding?.vvVideoView?.start()
                updateSeekBar()
                mViewModel?.setStatus()
                Glide.with(this@VideoPlayActivity).load(R.drawable.stop)
                    .into(mViewDataBinding?.ivPlayButton ?: return@OnClickListener)
            }
        })

        mViewDataBinding?.layoutMain?.setOnClickListener(View.OnClickListener {
            if (mViewModel?.videoDisPlay?.value ?: false) {
                mViewDataBinding?.ivPlayButton?.visibility = View.VISIBLE
                mViewModel?.setVideoDisPlay()
            } else {
                mViewDataBinding?.ivPlayButton?.visibility = View.GONE
                mViewModel?.setVideoDisPlay()
            }

        })
    }


    private val updateSeekBarRunnable: Runnable = object : Runnable {
        override fun run() {
            // 更新 SeekBar 的进度
            mViewDataBinding?.seekProcess?.setProgress(
                mViewDataBinding?.vvVideoView?.getCurrentPosition() ?: 0
            )
            // 每隔 100 毫秒更新一次进度条
            handler.postDelayed(this, 100)
        }
    }

    private fun updateSeekBar() {
        handler.postDelayed(updateSeekBarRunnable, 100)
    }

    override fun onDestroy() {
        super.onDestroy()
//        if (updateSeekBarRunnable != null) {
//            updateSeekBarRunnable = null
//            handler.removeCallbacks(updateSeekBarRunnable)
//        }
        handler.removeCallbacksAndMessages(null)
    }
}